Ext.ns('AmCheckView');

AmCheckView = Ext.extend(Ext.Panel, {

	// 头部工具栏
	topbar : null,
	// 条件搜索Panel
	searchPanel : null,
	// 数据展示Panel
	gridPanel : null,
	// GridPanel的数据Store
	store : null,
	// 构造函数
	constructor : function(_cfg) {

		Ext.applyIf(this, _cfg);
		// 初始化组件
		this.initUIComponents();
		// 调用父类构造
		AmCheckView.superclass.constructor.call(this, {
					id : 'AmCheckView',
					title : '电表抄表记录审核',
					region : 'center',
					layout : 'border',
					items : [this.searchPanel, this.gridPanel]
				});

	},// end of constructor

	// 初始化组件
	initUIComponents : function() {
		// 初始化搜索条件Panel
		this.searchPanel = new Ext.FormPanel({
			id : 'ElectricitySearchForm',
			height : 100,
			region : 'north',
			frame : false,
			border : false,
			padding : '10',
			layout : 'column',
			title : '查询条件',
			labelWidth : 60,
			enctype : 'multipart/form-data',
			fileUpload : true,
			url : __ctxPath + '/pdaweb/checkXLSFormatPdaAmMeterList.do',
			layoutConfig : {
				padding : '5',
				align : 'middle'
			},
			defaults : {
				columnWidth : 0.33,
				layout : 'form',
				border : false,
				style : 'padding-left:10px;',
				defaults : {
					xtype : 'textfield',
					width : 100
				}
			},
			items : [{
						items : [{
									fieldLabel : '抄表年月',
									xtype : 'monthfield',
									value : new Date(),
									editable : false,
									allowblank : false,
									name : 'Q_month_S_LK'
								}, {
									fieldLabel : '区  ID',
									xtype : 'textfield',
									name : 'Q_pdaAmMeter.districtId_S_LK'
								}]
					}, {
						items : [{
									fieldLabel : '电表编号',
									xtype : 'textfield',
									name : 'Q_amId_S_LK'
								}, {
									fieldLabel : '栋     ID',
									xtype : 'textfield',
									name : 'pdaAmMeterList.pdaAmMeter.buildingId'
								}]
					}, {
						items : [{
									fieldLabel : '房     ID',
									xtype : 'textfield',
									name : 'Q_pdaAmMeter.aparmentId_S_LK'
								}, {
									xtype : 'hidden',
									name : 'Q_status_S_NEQ',
									value : '1'
								}]
					}/*, {
						columnWidth : 0.25,
						layout : 'form',
						border : false,
						style : 'padding-left:10px;',
						items : [{
							fieldLabel : '记录状态',
							columnWidth : 0.25,
							xtype : 'combo',
							hiddenName : 'Q_status_S_EQ',
							valueField : 'status',
							displayField : 'statusData',
							mode : 'local',
							width : 100,
							editable : false,
							triggerAction : 'all',
							value : '',
							store : [['', '所有'], ['0', '待审核'], ['1', '已审核'],
									['2', '异常']]
						}]
					}*/]

		});// end of the searchPanel

		this.store = new Ext.data.Store({
					proxy : new Ext.data.HttpProxy({
								url : __ctxPath
										+ '/pdaweb/listPdaAmMeterList.do'
							}),
					reader : new Ext.data.JsonReader({
								root : 'result',
								totalProperty : 'totalCounts',
								fields : [{
											name : 'id',
											type : 'int'
										}, 'status', 'amId', 'lastDegree',
										'degree', 'fixedUsage', 'usage',
										'pdaAmMeter.districtId',
										'pdaAmMeter.buildingId',
										'pdaAmMeter.aparmentId', 'recorderId',
										'month']
							}),
					remoteSort : true
				});// end of the store

		// 选择的记录数

		var sm = new Ext.grid.CheckboxSelectionModel({
			handleMouseDown : Ext.emptyFn,
			listeners : {
				'beforerowselect' : function(s, i, k, r) {
					return r.get("status") != '2';
				}
			},
			renderer : function(v, c, r) {
				if (r.get("status") == '2') {
					return "<div style='padding:3px 0 0 3px;'><input type='checkbox' disabled /></div>";
				} else {
					return "<div class=\"x-grid3-row-checker\">&#160;</div>";
				}
			}
		});
		var cm = new Ext.grid.ColumnModel({
			columns : [sm, new Ext.grid.RowNumberer(), {
						header : '电表编号',
						dataIndex : 'amId'
					}, {
						header : '上月读数',
						dataIndex : 'lastDegree', // lastdegree
						renderer : function(val) {
							return val == -1 ? "" : val;
						}
					}, {
						header : '本月读数',
						dataIndex : 'degree'
					}, {
						header : '修正量',
						dataIndex : 'fixedUsage'
					}, {
						header : '本月用量',
						dataIndex : 'usage'
					}, {
						header : '区 ID',
						dataIndex : 'pdaAmMeter.districtId'
					}, {
						header : '栋 ID',
						dataIndex : 'pdaAmMeter.buildingId'
					}, {
						header : '房 ID',
						dataIndex : 'pdaAmMeter.aparmentId'
					}, {
						header : '抄表人',
						dataIndex : 'recorderId'
					}, {
						header : '抄表年月',
						dataIndex : 'month'
					}, {
						header : '管理',
						dataIndex : 'id',
						sortable : false,
						renderer : function(value, metadata, record, rowIndex,
								colIndex) {
							var d = record.data;
							var recordStr = d.amId + ";" + d.lastDegree + ";"
									+ d.degree + ";"
									+ record.get("pdaAmMeter.districtId") + ";"
									+ record.get("pdaAmMeter.buildingId") + ";"
									+ record.get("pdaAmMeter.aparmentId") + ";"
									+ d.recorderId + ";" + d.month + ";"
									+ d.status;
							var str = '';
							str += '&nbsp;<button title="修正" value="" class="btn-edit" '
									+ 'onclick="AmCheckView.fix('
									+ record.data.id + ');">&nbsp</button>';
							str += '&nbsp;<button title="查看详情" value=" " class="btn-showDetail" '
									+ 'onclick="AmCheckView.edit('
									+ record.data.id
									+ ",\'"
									+ recordStr
									+ '\');">&nbsp</button>';
							return str;
						}
					}],
			defaults : {
				sortable : true,
				menuDisabled : false
			}
		});// end of the cm

		this.topbar = new Ext.Toolbar({
					id : 'ElectricityFootBar',
					height : 30,
					bodyStyle : 'text-align:left',
					items : [new Ext.Button({
								iconCls : 'search',
								text : '查询',
								handler : function() {
									var searchPanel = Ext
											.getCmp('ElectricitySearchForm');
									var gridPanel = Ext
											.getCmp('ElectricityGrid');
									if (searchPanel.getForm().isValid()) {
										$search({
													searchPanel : searchPanel,
													gridPanel : gridPanel
												});
										var store = gridPanel.getStore();
										var count = store.getCount();
										for (var i = 0; i < count; i++) {
											if (store.getAt(i).get("status") == "1") {
												sm.selectRow(i);
											}
										}
									}
								}
							})]
				});

		this.gridPanel = new Ext.grid.GridPanel({
					id : 'ElectricityGrid',
					tbar : this.topbar,
					region : 'center',
					store : this.store,
					trackMouseOver : true,
					disableSelection : false,
					loadMask : true,
					//autoHeight : true,
					autoWidth:true,
					autoScroll :true,
					cm : cm,
					sm : sm,
					viewConfig : {
						showPreview : false,
						getRowClass : function(record, rowIndex, rowParams,
								store) {// 异常状态
							var st = record.data.status;
							if ('ERROR' == AmCheckView.getST(st)) {
								return 'x-grid-record-error';
							} else if ('WARN' == AmCheckView.getST(st)) {
								return 'x-grid-record-warning';
							} else {
								return '';
							}
						}
					},
					bbar : new Ext.PagingToolbar({
								pageSize : 25,
								store : this.store,
								displayInfo : true,
								displayMsg : '当前显示从{0}至{1}， 共{2}条记录',
								emptyMsg : "当前没有记录"
							})
				});
		var gridPanel = this.gridPanel;

		if (isGranted('_AMCheck')) {
			this.topbar.add(new Ext.Button({
						iconCls : 'btn-add',
						text : '审核',
						handler : function() {
							var grid = Ext.getCmp("ElectricityGrid");
							var selectRecords = grid.getSelectionModel()
									.getSelections();

							if (selectRecords.length == 0) {
								Ext.ux.Toast.msg("信息", "请选择记录！");
								return;
							}

							var warnIds = '';
							var waitIds = '';
							for (var i = 0; i < selectRecords.length; i++) {
								var st = selectRecords[i].data.status;
								if ('WAIT' == AmCheckView.getST(st)) {
									waitIds += selectRecords[i].data.id + ',';
								} else if ('WARN' == AmCheckView.getST(st)) {
									warnIds += selectRecords[i].data.id + ',';
								}
							}

							if (warnIds != '') {
								Ext.Msg.confirm('提示',
										'选择中包含黄颜色警示记录,你确定要审核通过吗？', function(
												btn, text) {
											if (btn == 'yes') {
												AmCheckView.check(waitIds
														+ warnIds);
											} else {
												return false;
											}
										});
							} else {
								if (waitIds != '') {
									AmCheckView.check(waitIds)
								}
							}
						}
					}));
		}
	}
});

// 编辑
AmCheckView.edit = function(recId, recordStr) {
	new AmCheckForm({
				recId : recId,
				checkMode : true,
				recordStr : recordStr
			}).show();
};
AmCheckView.checkAbnormal = function(status) {
	if (status == '2' || status == '3' || status == '4') {
		return true;
	}
	return false;
};
AmCheckView.getST = function(st) {
	return st == '0' ? 'WAIT' : st == '2' ? 'ERROR' : st == '3' || st == '4'
			? 'WARN'
			: 'OTHER';
}

AmCheckView.check = function(ids) {
	Ext.Ajax.request({
				url : __ctxPath + '/pdaweb/checkPdaAmMeterList.do?fileName='
						+ ids,
				method : 'post',
				waitMsg : '正在提交数据...',
				timeout: 30000,
				success : function(response) {
					Ext.ux.Toast.msg("操作信息", "审核成功");
					Ext.getCmp('ElectricityGrid').getStore().reload();
				},
				failure : function() {
					// Ext.Msg.alert("操作信息", "审核失败");
					Ext.MessageBox.show({
								title : '操作信息',
								msg : '审核失败',
								buttons : Ext.MessageBox.OK,
								icon : 'ext-mb-error'
							});
				}
			});
}

// 修正
AmCheckView.fix = function(id) {
	new FixForm({
				id : id
			}).show();
}